package com.yanqu.road.server.manager.peakstronghold;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.peakstronghold.config.PeakStrongHoldConfig;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PeakStrongholdActivityModelGroup {
    private int activityId;
    private Map<Integer, PeakStrongholdActivityModel> modelMap = new ConcurrentHashMap<>();

    public PeakStrongholdActivityModelGroup(int activityId) {
        this.activityId = activityId;
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public Map<Integer, PeakStrongholdActivityModel> getModelMap() {
        return modelMap;
    }

    public void setModelMap(Map<Integer, PeakStrongholdActivityModel> modelMap) {
        this.modelMap = modelMap;
    }

    public PeakStrongholdActivityModel getActivityModel(int unionGroup) {
        PeakStrongholdActivityModel model = modelMap.get(unionGroup);
        if (model == null) {
            synchronized (modelMap) {
                model = modelMap.get(unionGroup);
                if (model == null) {
                    PeakStrongHoldConfig config = CrossPeakStrongholdActivityMgr.getConfig(activityId);
                    if (config == null) {
                        ActivityInfo acInfo = CrossPeakStrongholdActivityMgr.reloadActivityInfo(activityId);
                        if (acInfo == null) {
                            return null;
                        }
                        config = CrossPeakStrongholdActivityMgr.getConfig(activityId);
                        if (config == null) {
                            return null;
                        }
                    }
                    model = new PeakStrongholdActivityModel(activityId, config, unionGroup);
                    modelMap.put(unionGroup, model);
                }
            }
        }

        return model;
    }

    public void save() {
        for (PeakStrongholdActivityModel activityModel : modelMap.values()) {
            activityModel.save();
        }
    }

    //定时检查活动阶段的变化
    public void checkStageTypeChange() {
        for (PeakStrongholdActivityModel activityModel : modelMap.values()) {
            activityModel.checkStageTypeChange();
            activityModel.checkNotifyServerIds();
        }
    }

    public void timerSyncTask(){
        for (PeakStrongholdActivityModel activityModel : modelMap.values()) {
            activityModel.timerSync();
        }
    }
}
